/*
 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
 * Copyright 2017 Ribose Inc. All Rights Reserved.
 *
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#ifndef CRYPTO_SM4_H
#define CRYPTO_SM4_H
#include <libecc/lib_ecc_config.h>
#include <libecc/words/words.h>

# ifdef  __cplusplus
extern "C" {
# endif

#define SM4_BLOCK_SIZE          16
#define SM4_KEY_SIZE            16
#define SM4_KEY_SCHEDULE        32

typedef struct sm4_rkey {
    unsigned int rk[SM4_KEY_SCHEDULE];
} sm4_rkey_t;

void sm4_gen_rkey(const u8 *key, sm4_rkey_t *ks);

void sm4_encrypt(const sm4_rkey_t *ks, const u8 *in, u8 *out);

void sm4_decrypt(const sm4_rkey_t *ks, const u8 *in, u8 *out);

typedef struct {
    u8 nonce[SM4_BLOCK_SIZE];
    sm4_rkey_t rk;
} sm4_context;

int sm4_init(sm4_context *ctx, const u8 *iv, int iv_len, const u8 *key, int key_len);

void sm4_encrypt_cbc(sm4_context *ctx, const u8 *in, int in_len, u8 *out);

void sm4_decrypt_cbc(sm4_context *ctx, const u8 *in, int in_len, u8 *out);

void sm4_process_ctr(sm4_context *ctx, const u8 *in, int in_len, u8 *out);

void sm4_finalize(sm4_context *ctx);

#ifdef __cplusplus
}
#endif

#endif
